﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	The GNU Library General Public License (LGPL) used in RapidWebDev is 
	intended to guarantee your freedom to share and change free software - to 
	make sure the software is free for all its users.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License (LGPL) for more details.

	You should have received a copy of the GNU Library General Public License (LGPL)
	along with this program.  
	If not, see http://www.rapidwebdev.org/Content/ByUniqueKey/OpenSourceLicense
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace RapidWebDev.FileManagement
{
	/// <summary>
	/// Permission configuration which defines the permission of all operations in file management.
	/// </summary>
	public class PermissionConfigurationSection : ConfigurationSection
	{
		/// <summary>
		/// Sets/gets file management permission sets.
		/// </summary>
		[ConfigurationProperty("settings")]
		public PermissionSettings @PermissionSettings
		{
			get { return this["settings"] as PermissionSettings; }
		}
	}

	/// <summary>
	/// File management permission configuration collection.
	/// </summary>
	public class PermissionSettings : ConfigurationElementCollection
	{
		/// <summary>
		/// When overridden in a derived class, creates a new System.Configuration.ConfigurationElement.
		/// </summary>
		/// <returns></returns>
		protected override ConfigurationElement CreateNewElement()
		{
			return new PermissionSetting();
		}

		/// <summary>
		/// Gets the element key for a specified configuration element when overridden in a derived class.
		/// </summary>
		/// <param name="element"></param>
		/// <returns></returns>
		protected override object GetElementKey(ConfigurationElement element)
		{
			PermissionSetting dataContextSetting = element as PermissionSetting;
			return dataContextSetting.Category + "." + dataContextSetting.OperationType.ToString();
		}

		/// <summary>
		/// Gets the type of the System.Configuration.ConfigurationElementCollection.
		/// </summary>
		public override ConfigurationElementCollectionType CollectionType
		{
			get { return ConfigurationElementCollectionType.BasicMap; }
		}

		/// <summary>
		/// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class.
		/// </summary>
		protected override string ElementName
		{
			get { return "setting"; }
		}
	}

	/// <summary>
	/// File management permission setting class.
	/// </summary>
	public class PermissionSetting : ConfigurationElement
	{
		/// <summary>
		/// Sets/gets file category name.
		/// </summary>
		[ConfigurationProperty("category", IsRequired = true)]
		public string Category
		{
			get { return this["category"] as string; }
			set { this["category"] = value; }
		}

		/// <summary>
		/// Sets/gets file management operation type.
		/// </summary>
		[ConfigurationProperty("operationType", IsRequired = true)]
		public FileManagementOperationType OperationType
		{
			get { return (FileManagementOperationType)this["operationType"]; }
			set { this["operationType"] = value; }
		}

		/// <summary>
		/// Sets/gets permission value.
		/// </summary>
		[ConfigurationProperty("permission", IsRequired = true)]
		public string Permission
		{
			get { return this["permission"] as string; }
			set { this["permission"] = value; }
		}
	}

	/// <summary>
	/// File management operation type.
	/// </summary>
	public enum FileManagementOperationType
	{
		/// <summary>
		/// Upload a file.
		/// </summary>
		Upload,

		/// <summary>
		/// Download a file.
		/// </summary>
		Download,

		/// <summary>
		/// Download thumbnail of a file.
		/// </summary>
		DownloadThumbnail,

		/// <summary>
		/// Delete an existing file.
		/// </summary>
		Delete
	}
}